package com.lagou.edu.controller;

import com.lagou.edu.dao.ResumeDao;
import com.lagou.edu.pojo.Resume;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @ClassName DemoController
 * @Description TODO
 * @Author xieguannan
 * @Date 2020/8/5 17:42
 */
@Controller
@RequestMapping("/demo")
public class DemoController {

    @Autowired
    private ResumeDao resumeDao;

    @RequestMapping("")
    public String homepage() {
        return "login";
    }

    @RequestMapping("/login")
    public ModelAndView login(String username, String password) {
        ModelAndView mav = new ModelAndView();
        if (username.equals("xieguannan") && password.equals("123")) {
            mav.addObject("resumes", resumeDao.findAll());
            mav.setViewName("list");
        } else {
            mav.setViewName("login");
        }
        return mav;
    }

    @RequestMapping("/add")
    public ModelAndView addResume(Resume resume) {
        resumeDao.save(resume);
        ModelAndView mav = new ModelAndView();
        mav.addObject("resumes", resumeDao.findAll());
        mav.setViewName("list");
        return mav;
    }

    @RequestMapping("/update")
    public ModelAndView updateResume(Resume resume) {
        resumeDao.save(resume);
        ModelAndView mav = new ModelAndView();
        mav.addObject("resumes", resumeDao.findAll());
        mav.setViewName("list");
        return mav;
    }

    @RequestMapping("/del")
    public ModelAndView delResume(Long id) {
        resumeDao.deleteById(id);
        ModelAndView mav = new ModelAndView();
        mav.addObject("resumes", resumeDao.findAll());
        mav.setViewName("list");
        return mav;
    }
}
